package com.samphanie.auiu.auth.validation.code.captcha.sms;

import cn.hutool.json.JSONObject;
import com.samphanie.auiu.auth.security.properties.AuthProperties;
import com.samphanie.auiu.auth.validation.code.captcha.Captcha;
import com.samphanie.auiu.auth.validation.code.captcha.service.AbstractValidateCodeProcessor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * 生成短信验证码
 *
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Component("smsCaptcha")
@RequiredArgsConstructor
public class SmsCodeProcessor extends AbstractValidateCodeProcessor<Captcha> {

    private final ISmsCodeSenderService smsCodeSenderService;

    /**
     * 发送校验码
     */
    @Override
    protected void send(ServletWebRequest request, Captcha captcha) throws Exception {
        String paramMobile = AuthProperties.DEFAULT_PARAMETER_NAME_MOBILE;
        String paramKey = AuthProperties.DEFAULT_PARAMETER_SMS_KEY;
        String mobile = request.getParameter(paramMobile);
        String key = request.getParameter(paramKey);

        JSONObject obj = new JSONObject();
        obj.put("code", captcha.getCode());
        smsCodeSenderService.send(key, mobile, String.valueOf(obj));
    }
}
